<?php
namespace app\admin\controller;

use app\admin\controller\AdminBaseController;
use think\Cache;
use think\Db;

/**
 * 系统配置
 * Class System
 */
class SystemController extends BaseController {

    /**
     * 站点配置
     */
    public function index() {
		if ($this->request->isPost()) {
            $site_config   = $this->request->post('site_config/a');
            $data['value'] = serialize($site_config);
            if (Db::name('system')->where('name', 'site_config')->update($data) !== false) {
                Cache::set('site_config', $site_config);
                $this->success('提交成功');
            } else {
                $this->error('提交失败');
            }
        }else{
        	if (Cache::has('site_config')) {
	            $site_config = Cache::get('site_config');
	        } else {
	            $site_config = Db::name('system')->field('value')->where('name', 'site_config')->find();
	            $site_config = unserialize($site_config['value']);
	        }
        	return $this->fetch('index', ['site_config' => $site_config]);
        }
    }
	//夺宝攻略
	public function duobao(){
		if ($this->request->isPost()) {
            $site_config =  input("content");
            $data['value'] = serialize($site_config);
            if (Db::name('system')->where('name', 'gl_config')->update($data) !== false) {
                Cache::set('gl_config', $site_config);
                $this->success('保存成功');
            } else {
                $this->error('保存失败');
            }
        }else{
        	if (Cache::has('site_config')) {
	            $site_config = Cache::get('gl_config');
	        } else {
	            $site_config = Db::name('system')->field('value')->where('name', 'gl_config')->find();
	            $site_config = unserialize($site_config['value']);
	        }
        	return $this->fetch('duobao', ['gl_config' => $site_config]);
        }
		return $this->fetch();
	}
	//协议
	public function xieyi(){
		if ($this->request->isPost()) {
            $site_config =  input("xieyi");
            $data['value'] = serialize($site_config);
            if (Db::name('system')->where('name', 'xieyi')->update($data) !== false) {
                Cache::set('xieyi', $site_config);
                $this->success('保存成功');
            } else {
                $this->error('保存失败');
            }
        }else{
        	if (Cache::has('xieyi')) {
	            $site_config = Cache::get('xieyi');
	        } else {
	            $site_config = Db::name('system')->field('value')->where('name', 'xieyi')->find();
	            $site_config = unserialize($site_config['value']);
	        }
        	return $this->fetch('xieyi', ['gl_config' => $site_config]);
        }
		return $this->fetch();
	}
		//看点
	public function info(){
		if ($this->request->isPost()) {
            $site_config =  input("info");
            $data['value'] = serialize($site_config);
            if (Db::name('system')->where('name', 'info')->update($data) !== false) {
                Cache::set('info', $site_config);
                $this->success('保存成功');
            } else {
                $this->error('保存失败');
            }
        }else{
        	if (Cache::has('info')) {
	            $site_config = Cache::get('info');
	        } else {
	            $site_config = Db::name('system')->field('value')->where('name', 'info')->find();
	            $site_config = unserialize($site_config['value']);
	        }
        	return $this->fetch('info', ['info' => $site_config]);
        }
		return $this->fetch();
	}	
	//联系我们
	public function about(){
		if ($this->request->isPost()) {
            $site_config =  input("about");
            $data['value'] = serialize($site_config);
            if (Db::name('system')->where('name', 'about')->update($data) !== false) {
                Cache::set('about', $site_config);
                $this->success('保存成功');
            } else {
                $this->error('保存失败');
            }
        }else{
        	if (Cache::has('xieyi')) {
	            $site_config = Cache::get('about');
	        } else {
	            $site_config = Db::name('system')->field('value')->where('name', 'about')->find();
	            $site_config = unserialize($site_config['value']);
	        }
        	return $this->fetch('about', ['about_config' => $site_config]);
        }
		return $this->fetch();
	}
}
